package _工具包整理._日期转换;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;

import java.text.ParseException;
import java.util.*;

/**
 * 使用 lang3 转换日期格式
 * Date 是 java.util 里的类
 *
 *
 */
public class DateTra {

    public static String replaceBrackets(String inputString){
        if (inputString == null) return inputString;
        String outputString = inputString.replace("<","(");
        outputString = outputString.replace(">", ")");
        return outputString;
    }

    String[] acceptedFormats = DATETIME_FORMATS;
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    //    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_MS_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String[] DATETIME_FORMATS = {DATE_FORMAT, "MM/dd/yyy HH:mm:ss.SSS",
            "EEE MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz",
            "EEE, MMM dd HH:mm:ss yyyy zzz","EEE, dd MMM yyyy HH:mm:ss zzz", "EEE,dd MMM yyyy HH:mm:ss zzz","EEE, dd MMM yyyy HH:mm:sszzz","EEE, dd MMM yyyy HH:mm:ss", "EEE, dd-MMM-yy HH:mm:ss zzz",
            "yyyy/MM/dd HH:mm:ss.SSS zzz","yyyy/MM/dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss zzz","yyyy/MM/dd", "MM/dd/yyyy", "MM/dd/yyyy HH:mm:ss",
            "yyyy.MM.dd HH:mm:ss","yyyy-MM-dd HH:mm", "yyyy.MM.dd",
            "MMM dd yyyy HH:mm:ss. zzz", "MMM dd yyyy HH:mm:ss zzz","dd MM yyyy HH:mm:ss zzz",
            "dd.MM.yyyy HH:mm:ss zzz","dd.MM.yyyy; HH:mm:ss", "dd.MM.yyyy HH:mm:ss","dd.MM.yyyy zzz", "dd.MM.yyyy"};


    @Test
    public void main() throws ParseException {
        String realValue = "8/1/2015 00:03:18";
        realValue = replaceBrackets(realValue);
        Date date = null;
        date= DateUtils.parseDate(realValue, acceptedFormats);
        realValue = DateFormatUtils.format(date, DATETIME_MS_FORMAT) ;
        System.out.println(realValue);
    }

    @Test
    public void test() throws ParseException {
        Date date = DateUtils.parseDate("05-01-2021 01:02:03.456", "MM-dd-yyyy HH:mm:ss.SSS");
        String str = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss.SSS");
        System.out.println(str); // 输出 2021-05-01 01:01:01
    }
}
